package com.ksd1.notes;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.ksd1.notes.service.TimeTaskService;
import com.ksd1.notes.util.TimeTool;
import com.ksd1.notes.util.YDTime;

public class RemainClock extends BaseActivity implements View.OnClickListener{
    Button clock_setTime;
    Button clock_cancel;
    Button clock_ok;
    TextView clock_hour;
    TextView clock_minute;
    EditText clock_title;
    TextView clock_help;
    LinearLayout clock_help_view;

    private int hour;
    private int minute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initVariables() {
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_remain_clock);
        clock_setTime = findViewById(R.id.clock_setTime);
        clock_cancel = findViewById(R.id.clock_cancel);
        clock_ok = findViewById(R.id.clock_ok);
        clock_hour = findViewById(R.id.clock_hour);
        clock_minute = findViewById(R.id.clock_minute);
        clock_title = findViewById(R.id.clock_title);
        clock_help = findViewById(R.id.clock_help);
        clock_help_view = findViewById(R.id.clock_help_view);

        clock_setTime.setOnClickListener(this);
        clock_cancel.setOnClickListener(this);
        clock_ok.setOnClickListener(this);
        clock_help.setOnClickListener(this);

        Toolbar toolbar = findViewById(R.id.clock_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);//显示返回箭头
            actionBar.setDisplayShowHomeEnabled(false);//不显示名字
        }
    }

    @Override
    protected void loadData() {
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String content = intent.getStringExtra("content");
        if(title != null) clock_title.setText(title);
        if(content == null) return;

        TimeTool.mTime mtime = TimeTool.getInstance().rformatTime(content);
        hour = mtime.hour;
        minute = mtime.minute;
        clock_hour.setText(hour+"");
        clock_minute.setText(minute+"");
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.clock_setTime:
                TimePickerDialog time=new TimePickerDialog(RemainClock.this, new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        RemainClock.this.AlarmClock(hourOfDay, minute);
                    }
                }, YDTime.getInstance().getHour(), YDTime.getInstance().getMinute(), true);


                time.show();
                break;
            case R.id.clock_cancel:
//                测试代码，打开通知
                /*Intent i = new Intent("com.ksd1.sendMessage");
                sendBroadcast(i);*/
                finish();
                break;
            case R.id.clock_ok:
                if(check()){
                    chooseForUpdate();
                }else{
                    Toast.makeText(this, "时间不正确或标题为空！！！", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.clock_help:
                if(clock_help_view.getVisibility() == View.GONE)
                    clock_help_view.setVisibility(View.VISIBLE);
                else
                    clock_help_view.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 1、判断当前是否已经有定时器
     * 2、用户选择是否更新
     */
    private void chooseForUpdate(){
        if(TimeTool.getInstance().getTiming()){//如果存在，需要进行判断
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("已有一个定时任务！！");
            dialog.setMessage("待办：" + TimeTool.getInstance().getField("title") + "\n时间：" + TimeTool.getInstance().getField("content") + "\n需要清除之前的记录吗？");
            dialog.setCancelable(false);
            dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {//用户点击确定
                    //1、先暂停服务
                    mstopService();
                    //2、开启新的定时服务
                    mstartService();
                }
            });

            dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "您取消了提醒", Toast.LENGTH_SHORT).show();
                }
            });

            dialog.show();
        }else{
            mstartService();
        }
    }

    /**
     * 开启一个定时服务
     */
    private void mstartService(){
        Intent intent = new Intent(this, TimeTaskService.class);
        intent.putExtra("title", clock_title.getText().toString());
        intent.putExtra("hour", hour);
        intent.putExtra("minute", minute);
        startService(intent);
        finish();
    }

    private void mstopService(){
        Intent intent = new Intent(this, TimeTaskService.class);
        stopService(intent);
    }

    /**
     * 用户点击确定验证此时是否合法
     * @return 是否合法
     */
    private Boolean check(){
        //当前时间要比设置的时间小
        if(YDTime.getInstance().getHour() > hour){
            return false;
        }
        if(YDTime.getInstance().getHour() == hour && YDTime.getInstance().getMinute() >= minute){
            return false;
        }
        String title = clock_title.getText().toString();
        if(title.equals("")) return false;
        return true;
    }

    /**
     * 相隔time的时间发送一条通知
     * @param hour 时
     * @param minute 分
     */
    private void AlarmClock(int hour, int minute){

        //1、计算到达该时间的时间差
        long time = YDTime.getInstance().getTimeStamp(hour, minute);

        if(time <= 0){
            Toast.makeText(this, "时间已过", Toast.LENGTH_SHORT).show();
        }else{
            this.hour = hour;
            this.minute = minute;
            clock_hour.setText(hour < 10 ? "0"+hour:hour+"");
            clock_minute.setText(minute< 10 ? "0"+minute:minute+"");
        }
    }
}
